package com.flashflexpro.flexdirector.console.icons{
    import com.flashflexpro.flexdirector.console.icons.halo.FlexHaloIcons;
    import com.flashflexpro.flexdirector.console.icons.spark.FlexSparkIcons;
    import com.flashflexpro.flexdirector.model.DtoFlexComInst;

    public class DirectorComInstIcons{


        [Embed(source="unknown.png")]
        private static var _unknown:Class

        public static function getIcon( dto:DtoFlexComInst ):Class{
            try{
                var rt:Class = getIconByClassName( dto.className.split( " " )[ 0 ] );
                if( rt != null ){
                    return rt;
                }
                var len:int = dto.superClassNames.length;
                for( var j:int = 0; j < len; j++ ){
                    rt = getIconByClassName( dto.superClassNames[j] as String );
                    if( rt != null ){
                        return rt;
                    }
                }
            }
            catch( e:Error ){
            }
            return _unknown;
        }

        private static function getIconByClassName( className:String ):Class{
            if( className.indexOf( "spark.components." ) == 0 ){
                return FlexSparkIcons.getInst()[ getLastClassName( className ).toLowerCase() ];
            } else if( className.indexOf( "mx." ) == 0 ){
                return FlexHaloIcons.getInst()[ getLastClassName( className ).toLowerCase() ];
            }
            return null;
        }

        private static function getLastClassName( c:String ):String{
            if( c.indexOf( "::" ) > 0 ){
                return c.split( "::" )[ 1 ];
            }
            else{
                var arr:Array = c.split( "." );
                return arr[ arr.length - 1 ];
            }
        }
    }
}
